package com.bd.volkswagen.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.domain.dto.device.DeviceDto;
import com.bd.scm.domain.dto.device.DzStDeviceDto;
import com.bd.scm.domain.dto.device.GetDeviceDto;
import com.bd.scm.domain.dto.door.DoorDto;
import com.bd.scm.domain.models.device.DzStDevice;
import com.bd.scm.domain.models.device.JuserKqRecord;
import com.bd.scm.domain.models.device.ShowStDevice;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.device.service.StDeviceService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.DateUtils;
import com.bd.utils.StringUtils;
import com.bd.utils.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Mason
 * @since 2019-09-03
 */
@RestController
@RequestMapping("/api/stDevice")
public class DeviceController extends BaseController {

    @Resource
    private StDeviceService stDeviceService;

    /**
     * 获取设备列表
     *
     * @param doorDto
     * @return
     */
    @PostMapping("getStDeviceList")
    public Result getStDeviceList(@RequestBody DoorDto doorDto) {
        return ResultUtil.ok(stDeviceService.getStDeviceList(doorDto));
    }

    /**
     * 设备管理界面--获取设备列表.
     *
     * @param getDeviceDto
     * @return
     */
    @PostMapping("getDevicePages")
    public Result getDevicePages(@RequestBody GetDeviceDto getDeviceDto) {
        IPage<ShowStDevice> page = stDeviceService.getDevicePages(getDeviceDto);
        return ResultUtil.ok(stDeviceService.getDevicePages(getDeviceDto));
    }

    /**
     * 获取设备最大编号+1.
     *
     * @return 现有最大编号+1
     */
    @PostMapping("getMaxIdFromDevices")
    public Result getMaxIdFromDevices() {
        String maxId = stDeviceService.getMaxIdFromDevices();
        return ResultUtil.ok(maxId);
    }

    /**
     * 设备管理界面--获取设备列表.
     *
     * @param deviceDto 新增的设备信息
     * @return 结果
     */
    @PostMapping("saveDevice")
    public Result saveDevice(@RequestBody DeviceDto deviceDto) {
        Result result = Result.ok("新增成功");
        try {
            stDeviceService.saveDevice(deviceDto);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }

    /**
     * 修改设备——获取设备信息.
     *
     * @param deviceDto 设备信息
     * @return 结果
     */
    @PostMapping("getDeviceByBh")
    public Result getDeviceByBh(@RequestBody DeviceDto deviceDto) {
        Result result = null;
        try {
            DeviceDto deviceInfo = stDeviceService.getDeviceByBh(deviceDto);
            result = Result.ok(deviceInfo);
            if (null == deviceInfo) {
                result = Result.error("获取设备信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }

    /**
     * 修改设备.
     *
     * @param deviceDto 修改的设备信息
     * @return 结果
     */
    @PostMapping("editDevice")
    public Result editDevice(@RequestBody DeviceDto deviceDto) {
        Result result = Result.ok("修改成功");
        try {
            stDeviceService.editDevice(deviceDto);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }

    /**
     * 删除设备.
     *
     * @param deviceDto 要删除设备的设备信息
     * @return 结果
     */
    @PostMapping("deleteDevice")
    public Result deleteDevice(@RequestBody DeviceDto deviceDto) {
        Result result = Result.ok("修改成功");
        try {
            stDeviceService.deleteDevice(deviceDto);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }


    // 大众考勤 设备开始

    // 查询
    @PostMapping("getDzStDeviceList")
    public Result getDzStDeviceList(@RequestBody DzStDeviceDto dzStDeviceDto) {
        Result result = null;
        try {
            Page<DzStDevice> page = new Page<>(dzStDeviceDto.getPageNumber(), dzStDeviceDto.getPageSize());
            IPage<DzStDevice> pageList = this.stDeviceService.getDzStDeviceList(page, dzStDeviceDto);
            result = ResultUtil.ok(pageList);
        }catch (Exception e){
            e.printStackTrace();
            result = ResultUtil.error("获取数据失败");
        }
        return  result;
    }


    // 添加
    @PostMapping("insertDzStDevice")
    public Result insertDzStDevice(@RequestBody DzStDeviceDto dzStDeviceDto) {
        Result result = null;
        try {
           // 设备名称
           if(StringUtils.isEmpty(dzStDeviceDto.getMc())){
               return Result.error("设备名称为空");
           }
            String dzStDevice = stDeviceService.insertDzStDevice(dzStDeviceDto);
            if (null == dzStDevice) {
                result = Result.ok("添加设备成功");
            }else{
                result = Result.error("添加设备失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }

    // 修改
    @PostMapping("updateDzStDevice")
    public Result updateDzStDevice(@RequestBody DzStDeviceDto dzStDeviceDto) {
        Result result = null;
        try {
            // 设备名称
            if(StringUtils.isEmpty(dzStDeviceDto.getMc())){
                return Result.error("设备名称为空");
            }
            String dzStDevice = stDeviceService.updateDzStDevice(dzStDeviceDto);
            if (null == dzStDevice) {
                result = Result.ok("修改设备成功");
            }else{
                result = Result.error("修改设备失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }

    // 删除
    @PostMapping("deleteDzStDevice")
    public Result deleteDzStDevice(@RequestBody DzStDeviceDto dzStDeviceDto) {
        Result result = null;
        try {
            // 设备唯一值
            if(StringUtils.isEmpty(dzStDeviceDto.getBh())){
                return Result.error("设备ID为空");
            }
            String dzStDevice = stDeviceService.deleteDzStDevice(dzStDeviceDto);
            if (null == dzStDevice) {
                result = Result.ok("删除设备成功");
            }else{
                result = Result.error("删除设备失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("程序内部错误");
        }
        return result;
    }

    /**
     * 手机打卡数据保存
     * @param juserKqRecord 设备唯一的ID  mJdevid
     * @return
     */
    @PostMapping("saveJuserKqRecord")
    public Result saveJuserKqRecord(@RequestBody JuserKqRecord juserKqRecord, HttpServletRequest request) {
        Result result = null;
        //获取登录人ID
        JSONObject user = null;
        try {
            //添加操作人ID
            if (ObjectUtil.isEmpty(juserKqRecord.getJlogUserId())) {
                return Result.error("用户信息不能为空");
            }
//            user = (JSONObject) request.getSession().getAttribute("user");
//            juserKqRecord.setJlogUserId(user.getString("USERSERIAL"));
//            juserKqRecord.setJlogUserId("75276");
            String currentDateTime = DateUtils.getCurrentDateTime();
            juserKqRecord.setJdatatime(currentDateTime);
//            if(StringUtils.isEmpty(juserKqRecord.getJlogUserId())){
//                result = Result.error("未取得登陆信息");
//                return result;
//            }
            try {
                if (!StringUtils.isEmpty(juserKqRecord.getJlogUserId()) &&
                        !StringUtils.isEmpty(String.valueOf(juserKqRecord.getMjDevid()))
                        && !StringUtils.isEmpty(juserKqRecord.getJdatatime())
                        && !StringUtils.isEmpty(String.valueOf(juserKqRecord.getJlogDir()))) {
                    String kqRecord = stDeviceService.saveJuserKqRecord(juserKqRecord);
                    if (null == kqRecord) {
                        result = Result.ok("保存成功");
                    } else {
                        result = Result.error(kqRecord);
                    }
                } else {
                    result = Result.error("参数无效");
                }
            } catch (Exception e) {
                e.printStackTrace();
                result = ResultUtil.error("保存异常");
            }
        } catch (NullPointerException n) {
            n.printStackTrace();
            result = Result.error("登录信息失效，请重新登录");
        }
        return result;
    }

    /**
     * 手机端定位当天上下班是否已打卡接口开发，返回前端上班是否已打卡，下班是否已打卡
     * @param request
     * @return
     */
    @PostMapping("getUserTtTaskCard")
    public Result getUserTtTaskCard(@RequestBody JuserKqRecord juserKqRecord, HttpServletRequest request) {
        DzStDeviceDto dzStDeviceDto = new DzStDeviceDto();
        Result result = null;
        //获取登录人ID
        JSONObject user = null;
        try {
            if (ObjectUtil.isEmpty(juserKqRecord.getJlogUserId())) {
                return Result.error("用户信息不能为空");
            }
            //添加操作人ID
//            user = (JSONObject) request.getSession().getAttribute("user");
//            dzStDeviceDto.setPkSysUser(user.getString("USERSERIAL"));
//            if(StringUtils.isEmpty(dzStDeviceDto.getPkSysUser())){
//                result = Result.error("未取得登陆信息");
//                return result;
//            }
            dzStDeviceDto.setPkSysUser(juserKqRecord.getJlogUserId());
            List<JSONObject> userTtTaskCard = this.stDeviceService.getUserTtTaskCard(dzStDeviceDto);
            result = Result.ok(userTtTaskCard);
        } catch (NullPointerException n) {
            n.printStackTrace();
            result = Result.error("登录信息失效，请重新登录");
        }
        return result;
    }

    @PostMapping("userConfirm")
    public Result userConfirm(@RequestBody JuserKqRecord userInfo) {
        stDeviceService.userConfirm(userInfo.getJlogUserId());
        return Result.ok("确认成功");
    }
}

